﻿using Hotel.Model;
using MD5Hash;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class MemberDAL
    {
        public MemberDAL(HotelConText hotelConText)
        {
            HotelConText = hotelConText;
        }

        public HotelConText HotelConText { get; }

        /// <summary>
        /// 会员增加
        /// </summary>
        /// <param name="info">会员实体</param>
        /// <returns>返回影响的行数</returns>
        public int MemberAdd(MemberModel info)
        {
            try
            {

                info.Pwd = info.Pwd.GetMD5();
                HotelConText.members.Add(info);
                return HotelConText.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 判断手机和身份证是否重复
        /// </summary>
        /// <param name="Phone">手机号</param>
        /// <param name="Card">身份证</param>
        /// <returns></returns>
        public int SearchPhoneOrCard(string Phone, string Card)
        {
            try
            {
                int res = HotelConText.members.Where(c => c.Phone == Phone).Count();
                if (res > 0)
                {
                    return -1;//表示手机号重复
                }
                int count = HotelConText.members.Where(c => c.Card == Card).Count();
                if (count > 0)
                {
                    return -2; //表示身份证重复
                }
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        /// <summary>
        /// 会员等级的增加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int AddMemberLevel(MemberLevelModel info)
        {
            try
            {
                HotelConText.levelModels.Add(info);
                return HotelConText.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 判断等级名称是否重复
        /// </summary>
        /// <param name="levelName">等级名称</param>
        /// <returns></returns>
        public int SearchLevelName(string levelName)
        {
            try
            {
                int res = HotelConText.levelModels.Where(c => c.LevelName == levelName).Count();
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取会员等级列表
        /// </summary>
        /// <returns></returns>
        public List<MemberLevelModel> MemberLevelsList()
        {
            try
            {
                return HotelConText.levelModels.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="id">会员编号</param>
        /// <param name="name">会员名称</param>
        /// <param name="levelId">等级名称</param>
        /// <returns></returns>

        public List<MemberModel> MemberList(int? id = null, string? name = "", int? levelId = null)
        {
            try
            {
                //查询所有显示的数据
                var list = HotelConText.members.Where(c => c.IsDeleted == false).ToList();
                //根据条件查询
                if (id != null)
                {
                    list = list.Where(c => c.Id == id).ToList();
                }

                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(c => c.Name.Contains(name)).ToList();
                }

                if (levelId != null)
                {
                    list = list.Where(c => c.LevelId == levelId).ToList();
                }

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据id删除会员信息
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int DelMemeberItem(int id)
        {
            try
            {
                //1.根据id找到对应的实体
                var info = HotelConText.members.Find(id);
                //2.修改状态
                info.IsDeleted = true;

                //3.更新数据库房
                return HotelConText.SaveChanges();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据会员id获取会员实体 返填+详情展示
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns></returns>
        public MemberModel GetMemberInfo(int id)
        {
            try
            {
                //根据id获取会员实体
                var info = HotelConText.members.Find(id);
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int UpdatMemberInfo(MemberModel info)
        {
            try
            {
                //1.根据id获取旧实体
                // var oldInfo = db.Members.Find(info.Id);
                //2.修改
                //oldInfo.LevelId = info.LevelId;
                //oldInfo.Money = info.Money;
                //oldInfo.Name = info.Name;
                //oldInfo.Phone = info.Phone;
                //oldInfo.Points = info.Points;
                //oldInfo.Pwd = info.Pwd;
                //oldInfo.Sex = info.Sex;
                //oldInfo.UpdateTime = DateTime.Now;

                //不能修改成已经存在人的信息
                int res = HotelConText.members.Count(c => c.Card == info.Card && c.Phone == info.Phone && c.Id != info.Id);
                if (res > 0)
                {
                    return -1;
                }

                HotelConText.members.Update(info);
                //3.更新数据库
                return HotelConText.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }








        /// <summary>
        /// 会员权益添加
        /// </summary>
        /// <param name="memberRight"></param>
        /// <returns></returns>
        public int MemberRightAdd(MemberRight memberRight)
        {

            HotelConText.rights.Add(memberRight);
            return HotelConText.SaveChanges();

        }



        /// <summary>
        /// 判断会员权益名称是否重复
        /// </summary>
        /// <param name="rightName"></param>
        /// <returns></returns>
        public int MemberRightName(string rightName)
        {
            int res = HotelConText.rights.Where(x => x.RightName == rightName).Count();
            return res;


        }


        /// <summary>
        /// 会员权益显示
        /// </summary>
        /// <param name="count">总条数</param>
        /// <param name="page">总页码</param>
        /// <param name="rightName">名称</param>
        /// <param name="createtiem">时间</param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        public List<MemberRight> MemberRightLiist(out int count, out int page, string? rightName = "", DateTime? date1 = null, DateTime? date2 = null, int index = 1, int size = 2)
        {

            List<MemberRight> list = HotelConText.rights.Where(x => x.IsDeleted == false).ToList();
            if (!string.IsNullOrEmpty(rightName))
            {
                list = list.Where(x => x.RightName.Contains(rightName)).ToList();
            }

            if (date1 != null && date2 != null)
            {

                list = list.Where(x => x.CreateTime >= date1 && x.CreateTime <= date2.Value.AddDays(1)).ToList();
            }
            count = list.Count;
            page = (int)Math.Ceiling(count * 1.0 / size);
            //分页
            list = list.OrderBy(x => x.id).Skip((index - 1) * size).Take(size).ToList();

            return list;

        }


        /// <summary>
        /// 修改会员权益的状态
        /// </summary>
        /// <param name="id">权益id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public int UpdateMemberRightState(int id, int state)
        {
            var info = HotelConText.rights.Find(id);
            //2 修改状态
            info.State= state;
            info.UpdateTime= DateTime.Now;
            return HotelConText.SaveChanges();

        }











    }
}
